package com.tanhua.server.controller;

import com.tanhua.domain.User;
import com.tanhua.domain.UserInfo;
import com.tanhua.dubbo.api.UserApi;
import com.tanhua.dubbo.api.UserInfoApi;
import com.tanhua.server.service.UserFreezeService;
import com.tanhua.server.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Map;

@RestController
@RequestMapping("user")
public class LoginController {
    @Autowired
    private UserService userService;

    @Autowired
    private UserFreezeService userFreezeService;

    @Autowired
    private UserApi userApi;

    /**
     * 发送验证码逻辑
     * @param map
     * @return
     */
    @PostMapping("/login")
    public ResponseEntity login(@RequestBody Map map){//body类型参数，通过@RequestBody转化对象，可以通过实体类或者map接受参数
        //获取phone手机号
        String phone = (String)map.get("phone");
        //当用户freezingRange为1时，冻结登录
        User user = userApi.findByMobile(phone);
        if(user != null){
            userFreezeService.checkUserStatus(1, user.getId());
        }
        //发送验证码
        userService.sendMsg(phone);
        //返回200响应码
        return ResponseEntity.ok(null);
    }

    /**
     * 登录验证逻辑
     * @param map
     * @return
     */
    @PostMapping("/loginVerification")
    public ResponseEntity loginVerification(@RequestBody Map map){//body类型参数，通过@RequestBody转化对象，可以通过实体类或者map接受参数
        //获取phone手机号
        String phone = (String)map.get("phone");
        String code = (String)map.get("verificationCode");
        //发送验证码
        Map voMap = userService.loginVerification(phone, code);
        //返回200响应码
        return ResponseEntity.ok(voMap);
    }



}
